const cacheNames = ["orderInfo", "checkCard", "checkCoupon", "checkParams", "checkVip", "ownPayList"]
export default {
  state: {
    // 影票订单信息
    orderInfo: {
      // ownPayList: [] // 支付方式列表
    },
    // 可使用的支付方式列表
    ownPayList: [],
    // 选中电影卡
    checkCard: {
      cardId: "",
      point: "", // 卡内点数
      money: "", // 卡内金额
      needPoint: "" // money = point * needPoint
    },
    // 选中的券信息
    checkCoupon: {},
    // 选中的vip卡信息
    checkVip: {},
    checkParams: null // 传参给券和卡列表
  },
  getters: {
    orderInfo: (state) => state.orderInfo,
    checkCard: (state) => state.checkCard,
    checkCoupon: (state) => state.checkCoupon,
    ownPayList: (state) => state.ownPayList, // state.orderInfo?.ownPayList.map((i) => i.key),
    checkParams: (state) => state.checkParams,
    checkVip: (state) => state.checkVip
  },
  mutations: {
    // 自动初始化
    INIT(state) {
      cacheNames.forEach((name) => {
        let data = uni.getStorageSync(name)
        if (data) state[name] = data
      })
    },
    // 可使用的支付方式列表
    SET_OWN_PAY_LIST(state, data) {
      state.ownPayList = data
      uni.setStorageSync("ownPayList", data)
    },
    // 订单信息
    SET_ORDER_INFO(state, data) {
      state.orderInfo = data
      uni.setStorageSync("orderInfo", data)
    },
    // 选中的卡
    SET_CHECK_CARD(state, data) {
      state.checkCard = data
      uni.setStorageSync("checkCard", data)
    },
    // 选中的券
    SET_CHECK_COUPON(state, data) {
      state.checkCoupon = data
      uni.setStorageSync("checkCoupon", data)
    },
    // 选中的vip卡信息
    SET_CHECK_VIP(state, data) {
      state.checkVip = data
      uni.setStorageSync("checkVip", data)
    },
    SET_CHECK_PARAMS(state, data) {
      state.checkParams = data
      uni.setStorageSync("checkParams", data)
    },
    // 清除订单信息
    CLEAR_ORDER_INFO(state) {
      state.orderInfo = {}
      state.ownPayList = []
      uni.removeStorageSync("orderInfo")
      uni.removeStorageSync("ownPayList")
    },
    CLEAR_CHECK_CARD(state) {
      state.checkCard = {
        cardId: "",
        point: "",
        money: "",
        needPoint: ""
      }
      uni.removeStorageSync("checkCard")
    },
    CLEAR_CHECK_COUPON(state) {
      state.checkCoupon = {}
      uni.removeStorageSync("checkCoupon")
    },
    CLEAR_CHECK_VIP(state) {
      state.checkVip = {}
      uni.removeStorageSync("checkVip")
    }
  },
  actions: {
    // 清除用户信息
    clearOrderInfo({ commit }) {
      commit("CLEAR_ORDER_INFO")
    },
    // 清除选卡信息
    clearCheckCard({ commit }) {
      commit("CLEAR_CHECK_CARD")
    },
    // 清除选卡信息
    clearCheckCoupon({ commit }) {
      commit("CLEAR_CHECK_COUPON")
    },
    // 清除选vip信息
    clearCheckVip({ commit }) {
      commit("CLEAR_CHECK_VIP")
    }
  }
}
